package com.foryou.wechat.controller;

import com.foryou.wechat.common.Constants;
import com.foryou.wechat.model.Query;
import com.foryou.wechat.model.dto.UserDTO;
import com.foryou.wechat.model.vo.FileVo;
import com.foryou.wechat.service.*;
import com.foryou.wechat.utils.FileUtil;
import com.foryou.wechat.utils.HttpContextUtils;
import com.foryou.wechat.utils.Response;
import com.foryou.wechat.utils.ValidatorUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by Lee.
 */

@RestController
@RequestMapping("/v/user")
public class UserController {

    private final UserService userService;
    private final UserFollowService userFollowService;
    private final SysOpinionService sysOpinionService;
    private final ProductionService productionService;
    private final ShootService shootService;

    @Value("${web.upload}")
    private String uploadPrefix;

    @Value("${web.url}")
    private String url;

    public UserController(UserService userService, UserFollowService userFollowService, SysOpinionService sysOpinionService, ProductionService productionService, ShootService shootService) {
        this.userService = userService;
        this.userFollowService = userFollowService;
        this.sysOpinionService = sysOpinionService;
        this.productionService = productionService;
        this.shootService = shootService;
    }

    @GetMapping("/getUserInfo")
    public Response getUserInfo() {
        return userService.getUserInfo(HttpContextUtils.getUserId());
    }

    @PostMapping("/updateUser")
    public Response updateUser(@RequestBody UserDTO userDTO) {
        //校验
        ValidatorUtils.validateEntity(userDTO);
        if (userService.updateUser(userDTO) > 0) {
            return Response.success();
        }
        return Response.failure("error");
    }

    @PostMapping("/updateUserAvatar")
    public Response updateUserAvatar(MultipartFile file) throws Exception{

        if (file.isEmpty()) {
            return Response.failure("error");
        }
        FileVo fileVo = FileUtil.uploadFile(uploadPrefix, file);
        String dataUrl = url.concat(fileVo.getFileName());
        userService.updateUserAvatar(HttpContextUtils.getUserId(), dataUrl);
        return Response.success(dataUrl);

    }

    @RequestMapping("/updateUserBack")
    public Response updateUserBack(@RequestParam("file") MultipartFile file)
            throws Exception{

        if (file.isEmpty()) {
            return Response.failure("error");
        }
        FileVo fileVo = FileUtil.uploadFile(uploadPrefix, file);
        String dataUrl = url.concat(fileVo.getFileName());
        return userService.updateUserBack(HttpContextUtils.getUserId(), dataUrl);

    }


    @GetMapping("/getUserIndex")
    public Response getUserIndex(Long userId) {
        if (userId == 0) {
            userId = HttpContextUtils.getUserId();
        }
        return userService.getUserIndex(userId);
    }

    @GetMapping("/getFollowList")
    public Response getFollowList(@RequestParam(defaultValue = "0") int start) {

        Long userId = HttpContextUtils.getUserId();
        return userService.getFollowList(start,userId);
    }

    @GetMapping("/getFansList")
    public Response getFansList(@RequestParam(defaultValue = "0") int start) {

        Long userId = HttpContextUtils.getUserId();
        return userService.getFansList(start,userId);
    }


    @GetMapping("/followUser")
    public Response followUser(Long userId) {
        return userFollowService.followUser(userId);
    }

    @GetMapping("/cancelUser")
    public Response cancelUser(Long userId) {
        userFollowService.cancelUser(userId);
        return Response.success();
    }

    @GetMapping("/getWorksImageList")
    public Response getWorksImageList(@RequestParam(defaultValue = "0") int start, Long userId) {
        return userService.getWorksImageList(start, userId);
    }

    @GetMapping("/getUserWorksList")
    public Response getUserWorksList(@RequestParam(defaultValue = "0") int start,Long userId) {
        Query query = getQuery(start, userId);
        return productionService.getProductionList(query);
    }

    @GetMapping("/getUserShootList")
    public Response getUserShootList(@RequestParam(defaultValue = "0") int start,Long userId) {
        Query query = getQuery(start, userId);
        return shootService.getShootList(query);
    }

    @GetMapping("/addOpinion")
    public Response addOpinion(String opinion) {
        return sysOpinionService.addOpinion(opinion, HttpContextUtils.getUserId());
    }

    @GetMapping("/checkUserInfo")
    public Response checkUserInfo() {
        Long userId = HttpContextUtils.getUserId();
        return userService.checkUserInfo(userId)?Response.success():Response.failure(Constants.EDIT_USER_INFO);
    }

    private Query getQuery(int start, Long userId) {
        Query query = new Query();
        query.setStart(start);
        if (userId == 0) {
            userId = HttpContextUtils.getUserId();
        }
        query.setUserId(userId);
        return query;
    }

}
